package com.how.warlock.entities;

import com.how.warlock.Game;
import com.how.warlock.art.Art;
import com.how.warlock.engine.AnimatedSprite;
import com.how.warlock.engine.AnimationTimer;
import com.how.warlock.player.Camera;
import com.how.warlock.player.Player;


public class PlayerEntity extends MovingEntity {
	
	private final static int ANIMATION_TIME = 200;

	private Game game;
	private Player player;
	
	private AnimatedSprite sprite;
	
	private AnimationTimer timer;
	
	private int currentFrame=0;
	private int currentDirection;
	private boolean moving;
	private final int framesPerDirection = 4;
	
	public PlayerEntity(Game game, int x, int y){
		super(x, y);
		this.game = game;
		this.sprite = new AnimatedSprite(Art.player);
		this.width = sprite.getHeight();
		this.height = sprite.getWidth();
		
		this.midHandle  = true;
		
		this.timer = new AnimationTimer(ANIMATION_TIME);
	}
	
	public void setMovement(double mx, double my){
		super.setMovement(mx,my);
		
		if (mx == 0 && my == 0) moving = false; else moving=true;
	}
	
	public void changeMovement(double deltax, double deltay){
		mx += deltax;
		my += deltay;
	}
	
	public void setDirection(int i){
		currentDirection = i;
	}
	
	private int calcFrames(){
		if (moving && timer.checkTime()){
			currentFrame++;
			if(currentFrame >= framesPerDirection) currentFrame = 0;
		} else if(!moving) currentFrame = 1;
		
		return currentFrame + currentDirection*framesPerDirection;
	}
	
	public void draw(){
		int tx = (int) x - Camera.getX();
		int ty = (int) y - Camera.getY();
		
		
		if(midHandle) { 
			tx -= sprite.getWidth();
			ty -= sprite.getHeight();
		}		
		
		sprite.draw(tx , ty, calcFrames());
	}
}
